package com.hongdee.crm.visit.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.crm.average.dao.IAverageDao;
import com.hongdee.crm.visit.dao.ITCrmVisitDao;
import com.hongdee.crm.visit.service.ITCrmVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/6/14.
 */
@Service
public class TCrmVisitServiceImpl implements ITCrmVisitService{
    @Autowired
    private ITCrmVisitDao itCrmVisitDao;
    private  String tabName="t_crm_car_visit";
    @Autowired
    private IHibernateDao iHibernateDao;

    @Override
    public Map newPage(String start, String limit, Map data) throws Exception {
        String sql = "select * from  t_crm_car_visit where 1=1 ";
        List<Object> params = new ArrayList();
        Map mapSelect = (HashMap) data.get("select");
        if (!mapSelect.get("value").toString().isEmpty()) {
            switch ((String) mapSelect.get("key")) {
                //vin
                case "1": {
                    sql+=" and vin=?  ";
                    params.add(mapSelect.get("value"));
                }
                break;
                //车牌号
                case "2": {
                    sql+=" and plate_num=?  ";
                    params.add(mapSelect.get("value"));
                }
                break;
                //车主姓名
                case "3": {
                    sql+=" and owner_name =? ";
                    params.add(mapSelect.get("value"));
                }
                break;
            }
        }
        Map maintainDate=(HashMap)data.get("survey");
        if (maintainDate.get("dateStart") != "") {
            String startTime = String.valueOf(maintainDate.get("dateStart"));
            startTime = startTime.substring(0, 10);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Long startTimeLong = sdf.parse(startTime).getTime();
            sql +=(" and date_start >= ? ");
            params.add(startTimeLong);
        }
        if (maintainDate.get("dateStop") != "") {
            String stopTime = String.valueOf(maintainDate.get("dateStop"));
            stopTime = stopTime.substring(0, 10) + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long stopTimeLong = sdf.parse(stopTime).getTime();
            sql+= (" and date_stop <= ?  ");
            params.add(stopTimeLong);
        }
        Object vin=data.get("vin");
        if (!StringUtils.isEmpty(vin)){
            sql +="and vin=?";
            params.add(vin);
        }
        Object ownerName=data.get("ownerName");
        if (!StringUtils.isEmpty(ownerName)){
            sql +="and owner_name=?";
            params.add(ownerName);
        }
        Object plateNum=data.get("plateNum");
        if (!StringUtils.isEmpty(plateNum)){
            sql +="and plate_num=?";
            params.add(plateNum);
        }
        Object complain=data.get("complain");
        if (!StringUtils.isEmpty(complain)){
            sql +="and complain=?";
            params.add(complain);
        }
        Object questionnaireName=data.get("questionnaireName");
        if (!StringUtils.isEmpty(questionnaireName)){
            sql +="and questionnaire_name=?";
            params.add(questionnaireName);
        }
        return this.itCrmVisitDao.page(sql, params.toArray(), start, limit);
    }
}
